package cn.eng.order.service.impl;

import cn.eng.basic.commonenum.ErrorCode;
import cn.eng.basic.exception.BusinessException;
import cn.eng.basic.utils.AjaxResult;
import cn.eng.basic.utils.CodeGenerateUtils;
import cn.eng.basic.utils.LoginContext;
import cn.eng.order.domain.OrderAddress;
import cn.eng.order.domain.OrderAdopt;
import cn.eng.order.dto.OrderAdoptDto;
import cn.eng.order.mapper.OrderAddressMapper;
import cn.eng.order.mapper.OrderAdoptMapper;
import cn.eng.order.service.IOrderAdoptService;
import cn.eng.basic.service.impl.BaseServiceImpl;
import cn.eng.pet.domain.Pet;
import cn.eng.pet.mapper.PetMapper;
import cn.eng.user.domain.Logininfo;
import cn.eng.user.domain.User;
import cn.eng.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
@SuppressWarnings("all")
public class OrderAdoptServiceImpl extends BaseServiceImpl<OrderAdopt> implements IOrderAdoptService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private OrderAdoptMapper orderAdoptMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;


    /**
     * 领养订单
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public AjaxResult submitOrder(OrderAdoptDto dto, HttpServletRequest request) throws Exception {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        Pet pet = petMapper.loadById(dto.getPetId());
        if (pet.getState()==0){
            throw new BusinessException(ErrorCode.UNDERCARRIAGE_ERROR.getCode());
        }
        //封装领养订单信息
        OrderAdopt orderAdopt = adopt2Order(dto,user,pet);
        //添加领养订单
        orderAdoptMapper.save(orderAdopt);
        //添加地址
        OrderAddress address = orderAddress2Info(dto,user,orderAdopt);
        orderAddressMapper.save(address);
        //修改宠物状态为下架
        pet.setState(0);
        pet.setUserId(user.getId());
        petMapper.update(pet);
        return AjaxResult.me();
    }
   /* *封装UserAddress地址数据**/
    private OrderAddress orderAddress2Info(OrderAdoptDto dto,User user,OrderAdopt orderAdopt) {
        OrderAddress address = new OrderAddress();
        address.setCreateTime(new Date());
        address.setOrderId(orderAdopt.getId());
        address.setOrderSn(orderAdopt.getOrderSn());
        address.setContacts(user.getUsername());
        address.setAreaCode(dto.getProvince()+dto.getCity()+dto.getDist());
        address.setAddress(dto.getStreet());
        address.setFullAddress(dto.getProvince()+dto.getCity()+dto.getDist()+dto.getStreet());
        address.setPhone(user.getPhone());
        return address;
    }

    /**封装OrderAdopt领养订单数据**/
    private OrderAdopt adopt2Order(OrderAdoptDto dto, User user,Pet pet){
        OrderAdopt order = new OrderAdopt();
        order.setDigest("对宠物"+pet.getName()+"的领养订单");
        order.setState(0);//默认待支付
        order.setPrice(pet.getSaleprice());
        //使用工具类获取 订单编号
        String orderSn = CodeGenerateUtils.generateOrderSn(dto.getPetId());
        order.setOrderSn(orderSn);
        //使用工具类获取 订单编号
        String paySn = CodeGenerateUtils.generateUnionPaySn();
        order.setPaySn(paySn);
        order.setPetId(dto.getPetId());
        order.setUserId(user.getId());
        order.setShopId(pet.getShopId());
        return order;

    }


}
